﻿
// script for controlling google timeline.

var days = new Array("Sunday", "Monday", "Tuesday",
                    "Wednesday", "Thursday", "Friday");
function drawGoogleTimeLine() {
    google.setOnLoadCallback(drawChart);
    drawChart();
}

function drawChart() {

    var container = document.getElementById('example5.1');
    var chart = new google.visualization.Timeline(container);
    var dataTable = new google.visualization.DataTable();
    var colorsArray = new Array('#f9fd01', '#32cd32', '#198d17', '#7b1113', '#06f2dd', '#0618f2',
 	                   '#ff00ff', '#9106f2', '#008080', '#000080');

    dataTable.addColumn({ type: 'string', id: 'day' });
    dataTable.addColumn({ type: 'string', id: 'Name' });
    dataTable.addColumn({ type: 'date', id: 'Start' });
    dataTable.addColumn({ type: 'date', id: 'End' });

    var i = 0;
    var j = 0;
    var rowIndex = 0;
    var colorIndex = 0;
    var rows = new Array();
    var daysArrays = new Array();
    var coursesColorsMap = new Array();
    var lessonsColors = new Array();
    for (i = 0; i < 7; i++) {
        daysArrays[i] = new Array();
    }
    for (i = 0; i < schedulesAsJson[currentRank].jsTable.length; i++) {
        if (schedulesAsJson[currentRank].jsTable[i].start == "" || schedulesAsJson[currentRank].jsTable[i].duration == 0) {
            continue;
        }
        var eventName = schedulesAsJson[currentRank].jsTable[i].courseName + " - " +
                        schedulesAsJson[currentRank].jsTable[i].groupRole + " " +
                        schedulesAsJson[currentRank].jsTable[i].groupNumber + " - " +
                        schedulesAsJson[currentRank].jsTable[i].room;
        var eventDayIndex = schedulesAsJson[currentRank].jsTable[i].day;
        var eventDay = days[eventDayIndex];
        var startTimeSplitted = schedulesAsJson[currentRank].jsTable[i].start.split(".");
        var eventEndTime = calculateEndTime(schedulesAsJson[currentRank].jsTable[i].start,
                                            schedulesAsJson[currentRank].jsTable[i].duration);
        var endTimeSplitted = eventEndTime.split(":");
        if (coursesColorsMap[schedulesAsJson[currentRank].jsTable[i].courseName] == undefined) {
            coursesColorsMap[schedulesAsJson[currentRank].jsTable[i].courseName] = colorsArray[colorIndex];
            colorIndex++;
        }
        daysArrays[eventDayIndex][daysArrays[eventDayIndex].length] = {
                                                                       "color": coursesColorsMap[schedulesAsJson[currentRank].jsTable[i].courseName],
                                                                       "event": [eventDay, eventName, new Date(0, 0, 0, startTimeSplitted[0],
                                                                                 startTimeSplitted[1], 0), new Date(0, 0, 0, endTimeSplitted[0],
                                                                                 endTimeSplitted[1], 0)]
                                                                      };
    }
   /* dataTable.addRows([
      ['Magnolia Room', 'Beginning JavaScript', new Date(0, 0, 0, 12, 0, 0), new Date(0, 0, 0, 13, 30, 0)],
      ['Magnolia Room', 'Intermediate JavaScript', new Date(0, 0, 0, 14, 0, 0), new Date(0, 0, 0, 15, 30, 0)],
      ['Magnolia Room', 'Advanced JavaScript', new Date(0, 0, 0, 16, 0, 0), new Date(0, 0, 0, 17, 30, 0)],
      ['Willow Room', 'Beginning Google Charts', new Date(0, 0, 0, 12, 30, 0), new Date(0, 0, 0, 14, 0, 0)],
      ['Willow Room', 'Intermediate Google Charts', new Date(0, 0, 0, 14, 30, 0), new Date(0, 0, 0, 16, 0, 0)],
      ['Willow Room', 'Advanced Google Charts', new Date(0, 0, 0, 16, 30, 0), new Date(0, 0, 0, 18, 0, 0)]]); */
    var chartColors = new Array();
    for (i = 0; i < 7; i++) {
        for (j = 0; j < daysArrays[i].length; j++) {
            rows[rowIndex] = daysArrays[i][j].event;
            chartColors[rowIndex] = daysArrays[i][j].color;
            rowIndex++;
        }
        if (daysArrays[i].length == 0) {
            rows[rowIndex] = [days[i], '', new Date(0, 0, 0, 7, 0, 0), new Date(0, 0, 0, 7, 0, 0)];
            chartColors[rowIndex] = '#ffffff';
            rowIndex++;
        }

    }
    dataTable.addRows(rows);
    var options = {
        colors: chartColors,
        avoidOverlappingGridLines: false
    };

    chart.draw(dataTable, options);
}
